import { observable, flow, action } from 'mobx';
import { getActivity, addActivityCustomer, getActivityData, addActivityPolicy } from '../services';

class Institution {
  @observable data = {};
  @observable pending = false;
  @observable submitStatus = false;
  @observable eventData = {};

  init = flow(function*(id) {
    this.pending = true;
    try {
      const data = yield getActivity(id);
      this.pending = false;
      this.data = data;
    } catch (e) {}
  });
  addCustomeer = flow(function*(values) {
    this.pending = true;
    try {
      const data = yield addActivityCustomer(values);
      if (data) {
        this.pending = false;
        this.submitStatus = false;
        return data;
      }
    } finally {
      this.pending = false;
      this.submitStatus = false;
    }
  });
  addPolicy = flow(function*(values) {
    this.pending = true;
    try {
      const data = yield addActivityPolicy(values);
      if (data) {
        this.pending = false;
        this.submitStatus = false;
        return data;
      }
    } catch(e){} finally {
      this.pending = false;
      this.submitStatus = false;
    }
  });
  getEventData = flow(function*(eventId) {
    const data = yield getActivityData(eventId);
    this.eventData = data;

  });
  @action
  submit = stop => {
    if (stop) {
      this.submitStatus = false;
      return;
    }
    this.submitStatus = true;
  };
  @action
  clear = () => {
    this.data = {};
    this.pending = false;
  };
  @action
  clearData = () => {
    this.eventData = {};
    this.pending = false;
  };
}

export default new Institution();
